<template>
  <section class="todoapp">
    <TodoHeader @add="add"></TodoHeader>
    <TodoMain :list="all" @del="del"> </TodoMain>
    <TodoFooter :list="list" @qingchu="qc" @check="ck"></TodoFooter>
  </section>
</template>

<script>
// 样式引入 + 导入组件并注册使用
import "./styles/base.css";
import "./styles/index.css";
import TodoFooter from "./components/TodoFooter.vue";
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";

export default {
  data() {
    return {
      type: "all",
      list: JSON.parse(localStorage.getItem("list")),
    };
  },
  methods: {
    del(id) {
      // console.log(id);
      this.list = this.list.filter((item) => item.id != id);
    },
    add(zi) {
      console.log(zi);
      this.list.push({
        id: +new Date(),
        name: "zi",
        isDone: false,
      });
    },
    qc() {
      this.list = this.list.filter((item) => !item.isDone);
    },
    ck(a) {
      // console.log(a);
      this.type = a;
    },
  },
  components: {
    TodoFooter,
    TodoHeader,
    TodoMain,
  },
  computed: {
    all() {
      if (this.type === "all") {
        console.log(123);
        return this.list;
      }
      if (this.type === "wei") {
        console.log(23);
        return this.list.filter((item) => item.isDone);
      }
      if (this.type === "yi") {
        console.log(3);
        return this.list.filter((item) => !item.isDone);
      }
    },
  },
  watch: {
    list: {
      handler(newarr) {
        localStorage.setItem("list", JSON.stringify(newarr));
      },
      deep: true,
    },
  },
};
</script>
